// pages/demo/demo.js
const utils = require('../../utils/util.js');

const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    scrollTop: true,
    scrollHeight: 0,
    loading: false,
    color: '#000',
    background: '#f8f8f8',
    show: false,
    animated: false,

    book: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (url) {
    
    this.setData({
      ['book.content']: '',
      ['book.next']: url,
      scrollHeight: wx.getSystemInfoSync().windowHeight
    })
    if(Object.keys(url).length != 0){
      this.getNextContent();
    }


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 获取爬虫信息
  getNextContent: function (event) {
    var that = this
    utils.httpGet('/demo/test', {
      url: that.data.book.next
    }, (data) => {
      if (!data.success) {
        wx.showToast({
          title: data.msg,
          icon: 'none'
        });
        return;
      }

      that.setData({
        scrollTop: !that.data.scrollTop,
        book: data.data
      });
    });
  },
  // 获取爬虫信息
  getPreContent: function (event) {
    var that = this
    utils.httpGet('/demo/test', {
      url: that.data.book.previous
    }, (data) => {
      if (!data.success) {
        wx.showToast({
          title: data.msg,
          icon: 'none'
        });
        return;
      }

      that.setData({
        scrollTop: !that.data.scrollTop,
        book: data.data
      });
    });
  },
  //navigation
  toggleAnimated() {
    this.setData({
      animated: !this.data.animated,
      show: !this.data.show
    })
  },
  //navigation
  showMenu() {
    this.setData({
      // animated: !this.data.animated,
      show: !this.data.show
    })
  }
})